﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class MoralityItemLogic : CommonLogic<MoralityItem, int>, IMoralityItemLogic
    {
        private ISchoolInfoLogic _schoolLogic;
        public MoralityItemLogic(ISchoolInfoLogic schoolLogic)
        {
            _schoolLogic = schoolLogic;
        }

        #region Implementation of ICommonLogic<MoralityItem, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(MoralityItem entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(MoralityItem entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public MoralityItem FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<MoralityItem> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<MoralityItem> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        #region Implementation of IMoralityItemLogic

        /// <summary>
        /// 获取德育项目
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<MoralityItemView> GetMoralityItemList(string schoolMark)
        {
            SchoolInfo school = _schoolLogic.FindBySchoolMark(schoolMark);
            int schoolYear = school.SchoolYear;
            int term = school.SchoolTerm;
            return GetMoralityItemList(schoolMark, schoolYear, term);
        }

        /// <summary>
        /// 获取德育项目
        /// </summary>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<MoralityItemView> GetMoralityItemList(string schoolMark, int schoolYear, int term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("SchoolYear", schoolYear);
            ht.Add("Term", term);
            return FindCustomer<MoralityItemView>("MoralityItem.GetMoralityItemList", ht);
        }

        /// <summary>
        /// 获取德育小类
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="typeID"></param>
        /// <returns></returns>
        public IList<MoralityItem> GetItemByType(string schoolMark, int typeID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("TypeID", typeID);
            return FindCustomer<MoralityItem>("MoralityItem.Find", ht);
        }

        /// <summary>
        /// 获取德育单项
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="itemID"></param>
        /// <returns></returns>
        public MoralityItem GetItemByID(string schoolMark, int itemID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("RID", itemID);
            return FindCustomerByID<MoralityItem>("MoralityItem.FindById", ht);
        }

        /// <summary>
        /// 删除项
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public int DeleteByTypeId(string schoolMark, int typeId)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("typeId", typeId);

            return DeleteCustomer("MoralityItem.DeleteByTypeId", table);
        }

        /// <summary>
        /// 删除德育项
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public int DeleteById(string schoolMark, int rid)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("rid", rid);

            return DeleteCustomer("MoralityItem.DeleteById", table);
        }
 

        /// <summary>
        /// 操作德育项 增删
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="listAdd"></param>
        /// <param name="listDel"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.Requires)]
        public int SaveMoralityItem(string schoolMark, List<MoralityItem> listAdd, List<MoralityItem> listDel)
        {
             int result = 0;
             try
             {
                 foreach (MoralityItem delItem in listDel)
                 {
                     DeleteById(schoolMark, delItem.RID);
                 }
                 foreach (MoralityItem addItem in listAdd)
                 {
                     Hashtable table = new Hashtable();
                     table.Add("SchoolMark", schoolMark);
                     table.Add("typeId", addItem.TypeID);
                     table.Add("historyId", addItem.HistoryID);
                     table.Add("typeName", addItem.Caption);
                     table.Add("type", addItem.Type);
                     table.Add("operator", addItem.Operator);
                     table.Add("param", addItem.Param);

                     int id = (int)InsertCustomer("MoralityItem.InsertItem", table);
                 }
                 result = 1;
             }
             catch (Exception e)
             {
                 result = 0;
             }
             return result;
        }

        #endregion
    }
}